import { show } from '@/utils/NotificationUtil';
import React from 'react';
let socket;
//创建websocket连接
export const connectChat = (name) => {
  if (typeof WebSocket == 'undefined') {
    console.log('您的浏览器不支持WebSocket');
  } else {
    //实现化WebSocket对象，指定要连接的服务器地址与端口 建立连接
    // socket = new WebSocket(`ws://120.24.168.122:8088/jokerpanBack/chat/${name}`);
    socket = new WebSocket(`ws://localhost:8088/jokerpanBack/chat/${name}`);
    //将初始化的socket装载到全局window对象中
    window.chatServer = socket;
    //打开事件
    socket.onopen = function () {
      console.log('Socket 已打开');
      //socket.send("这是来自客户端的消息" + location.href + new Date());
    };
    //关闭事件
    socket.onclose = function () {
      console.log('Socket已关闭');
    };
    //发生了错误事件
    socket.onerror = function () {
      // alert("Socket发生了错误");
      show('Socket发生了错误', '', 'error');
    };
    //窗口关闭后同时关闭socket
    window.unload = function () {
      socket.close();
    };
  }
};
